package com.atelier.plugin.projectWizard.util;

import com.intellij.ide.RecentProjectsManagerBase;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.ui.configuration.IdeaProjectSettingsService;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;

import java.io.File;

public class DefaultNamingUtil {
    private static final String BASE_NAME = "demo";

    public static synchronized String generateUniqueName(String defaultProjectPath) {
        String basePath = defaultProjectPath;

        String uniqueName = BASE_NAME;
        int count = 1;
        while (isNameAlreadyTaken(basePath, uniqueName)) {
            uniqueName = BASE_NAME + count;
            count++;
        }
        return uniqueName;
    }

    private static boolean isNameAlreadyTaken(String basePath, String name) {
        File baseDir = new File(basePath);
        File[] existingDirs = baseDir.listFiles();

        if (existingDirs != null) {
            for (File existingDir : existingDirs) {
                if (existingDir.isDirectory() && existingDir.getName().startsWith(name)) {
                    return true; // Found a directory with a similar name
                }
            }
        }
        return false;
    }
}
